<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <base href="/">
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<!--数据列表-->
<table class="layui-table" lay-data="{ url:'order/list', page:true, id:'datalist',toolbar:'#toptools'}" lay-filter="datalist">
    <thead>
    <tr>
        <th lay-data="{field:'orderid'}">订单ID</th>
        <th lay-data="{field:'code'}">订单编号</th>
        <th lay-data="{field:'userid'}">用户ID</th>
        <th lay-data="{field:'money'}">金额</th>
        <th lay-data="{field:'addtime'}">创建时间</th>
        <th lay-data="{field:'paymenttime'}">支付时间</th>
        <th lay-data="{field:'deliverytime'}">发货时间</th>
        <th lay-data="{field:'gettime'}">收货时间</th>
        <th lay-data="{field:'endtime'}">完成时间</th>
        <th lay-data="{field:'state'}">订单状态</th>
        <th lay-data="{field:'updatetime'}">更新时间</th>
        <th lay-data="{field:'delivery'}">是否发货</th>
        <th lay-data="{toolbar:'#linetools'}">操作</th>
    </tr>
    </thead>
</table>

<!--新增数据表单-->




<!--行内工具栏-->
<script id="linetools" type="text/html">

    <button type="button" class="layui-btn-danger" lay-event="Delete">删除</button>
    <button type="button" class="layui-btn-danger" lay-event="delivery">发货</button>
</script>

<!--头部工具栏-->
<script id="toptools" type="text/html">
    <div class="layui-form-item">
        <div class="layui-input-inline">
            <input type="text" name="text" placeholder="请输入查询内容" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-input-inline">
            <button type="button" class="layui-btn" lay-event="check">查询</button>
            <button type="button" class="layui-btn" lay-event="add">新增</button>
        </div>
    </div>
</script>

<script src="layui/layui.all.js"></script>

<script>
    const $ = layui.$;
    const table = layui.table;
    const form = layui.form;
    const layer = layui.layer;
    const upload = layui.upload;

    /*行内按钮点击事件*/
    table.on("tool(datalist)", function (obj) {
        let event = obj.event;
        let data = obj.data;
        console.log(data);
        if (event == "Delete") {
            layer.confirm("确认要删除吗", function () {
                $.ajax({
                    url: 'order/Delete',
                    data: { orderid: data.orderid },
                    success: function () {
                        layer.msg("删除成功", { time: 1800, icon: 1 });
                        table.reload("datalist");
                    }
                });
            });

        }else if(event=="delivery"){
            // 发货按钮点击事件
            console.log("哈吉米");
            layer.confirm("确认要发货吗？", function () {
                $.ajax({
                    url: 'order/delivery',
                    type: 'POST',
                    data: { orderid: data.orderid }, // 传递订单ID
                    success: function () {
                        layer.msg("发货成功", { time: 1800, icon: 1 });
                        table.reload("datalist"); // 刷新表格
                    },
                    error: function () {
                        layer.msg("发货失败", { time: 1800, icon: 2 });
                    }
                });
            });
        }

    });
    /*头部工具栏点击事件*/
    let currentData = null; // 定义一个全局变量，用于保存当前行的数据

    table.on("tool(datalist)", function (obj) {
        let event = obj.event;
        let data = obj.data;
        if (event == "Delete") {
            layer.confirm("确认要删除吗", function () {
                $.ajax({
                    url: 'order/Delete',
                    data: { orderid: data.orderid },
                    success: function () {
                        layer.msg("删除成功", { time: 1800, icon: 1 });
                        table.reload("datalist");
                    }
                });
            });
        }
        else if(event=="delivery"){
            layer.confirm("确定要发货吗",function(){
                $.ajax({
                    url:'order/delivery',
                    data:{orderid:data.orderid},
                    success:function(){
                        layer.msg("发货成功",{time:1800,icon:1});
                        table.reload("datalist");
                    }
                });
            });
        }
    });


    /*上传图片*/
    upload.render({
        elem: "#addupload",
        url: "file/upload",
        done: function (result) {
            let url = result.data[0].src;
            $("#addupload").attr("src", url);
            $("input[name='imgurl']").val(url);
        },
        error: function () {
            layer.msg("上传失败", { icon: 2 });
        }
    });


</script>
</body>
</html>